/*
 * PathComparator.java
 */

package xq.util;

import java.util.Comparator;

/**
 *
 * @author ThorntonRP
 */
public class PathComparator implements Comparator<String> {
	
	private final String fileSeparator;
	private boolean directoriesAreHigh;

	public PathComparator(String fileSeparator, boolean directoriesAreHigh) {
		this.fileSeparator = fileSeparator.replace("\\", "\\\\");
		this.directoriesAreHigh = directoriesAreHigh;
	}
	
	public int compare(String o1, String o2) {
		String[] path1 = o1.split(fileSeparator);
		String[] path2 = o2.split(fileSeparator);
		int last = Math.min(path1.length, path2.length) - 1;
		for (int i = 0; i < last; i++) {
			int result = path1[i].compareToIgnoreCase(path2[i]);
			if (result != 0) {
				return result;
			}
		}
		if (path1.length == path2.length) {
			return path1[last].compareToIgnoreCase(path2[last]);
		} else if (directoriesAreHigh) {
			return path1.length - path2.length;
		} else {
			return path2.length - path1.length;
		}
	}
}
